package com.kt.uokos.yimo.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;

import com.kt.uokos.yimo.dao.Coupon;
import com.kt.uokos.yimo.example.CouponExample;
import com.kt.uokos.yimo.mapper.CouponMapper;
import com.kt.uokos.yimo.service.CouponService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class CouponServiceImpl implements CouponService {

    @Resource
    private CouponMapper couponMapper;



    @Override
    public List<Coupon> querySelective(String name, Short type, Short status, Integer page, Integer limit, String sort, String order) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);

        return couponMapper.selectByExample(example);
    }

    public List<Coupon> queryNewUserList(int offset, int limit,String sort,String order){
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo((short) 0).andStatusEqualTo((short)0).andDeletedEqualTo(false);
        example.setOrderByClause(sort+" "+order);
        PageHelper.startPage(offset,limit);
        return couponMapper.selectByExample(example);
    }

    @Override
    public Coupon queryById(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer countCoupon(Integer couponId) {
        CouponExample example = new CouponExample();
        example.or().andIdEqualTo(couponId).andDeletedEqualTo(false);
        return (int)couponMapper.countByExample(example);
    }

    @Override
    public void add(Coupon coupon) {
        coupon.setAddTime(LocalDateTime.now());
        coupon.setUpdateTime(LocalDateTime.now());
        coupon.setDeleted(false);
        coupon.setStatus((short) 0);
        couponMapper.insertSelective(coupon);
    }

    @Override
    public void delete(Integer id) {
        couponMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Coupon coupon) {
        couponMapper.updateByPrimaryKey(coupon);
    }



}
